#include "4g_sms_modem.h"
#include "4g_platform.h"

#define INI_PATH  "/home/nbiot/pengjiawei/sms-modem/configure.ini"

int  get_config(msq_ctx_t *msq){

        char                    section[128];
        dictionary              *ini=NULL;
        const   char            *str;

        //memset(msq,0,sizeof(*msq));
        /*加载ini文件，将数据存到ini结构中*/
        ini = iniparser_load(INI_PATH);
        if(!ini)
        {
                printf("no found config.ini\n");
                return -1;
        }
        printf("platform=%s\n",msq->p_ctx.platform);
        /*获取配置文件的value*/

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:clientid",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        printf("str=%s\n",str);
        strncpy(msq->p_ctx.clientid,str,strlen(str));
        printf("clientid = %s\n",msq->p_ctx.clientid);

#if 1
        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:username",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->p_ctx.username, str,strlen(str));
        printf("username = %s\n", msq->p_ctx.username);
#endif
#if 1
        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:password",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->p_ctx.password, str, strlen(str));
        printf("password = %s\n", msq->p_ctx.password);
#endif
        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:pub_topic",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        snprintf(msq->pub_ctx.pub_topic,sizeof(msq->pub_ctx.pub_topic),str);
        printf("pub_topic = %s\n",msq->pub_ctx.pub_topic);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:sub_topic",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        snprintf(msq->sub_ctx.sub_topic,sizeof(msq->sub_ctx.sub_topic),str);
        printf("sub_topic = %s\n",msq->sub_ctx.sub_topic);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:port",msq->p_ctx.platform);
        msq->p_ctx.port = iniparser_getint(ini, section,-1);
        printf("port = %d\n",msq->p_ctx.port);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:keepalive",msq->p_ctx.platform);
        msq->p_ctx.keepalive = iniparser_getint(ini, section,-1);
        printf("keepalive = %d\n",msq->p_ctx.keepalive);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:sub_qos",msq->p_ctx.platform);
        msq->sub_ctx.sub_qos = iniparser_getint(ini, section,-1);
        printf("sub_qos = %d\n",msq->sub_ctx.sub_qos);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:pub_qos",msq->p_ctx.platform);
        msq->pub_ctx.pub_qos = iniparser_getint(ini, section,-1);
        printf("pub_qos = %d\n",msq->pub_ctx.pub_qos);

        memset(section,0,sizeof(section));
        snprintf(section,32,"logger:loglevel");
        msq->p_ctx.loglevel = iniparser_getint(ini, section,-1);
        printf("loglevel = %d\n",msq->p_ctx.loglevel);

        memset(section,0,sizeof(section));
        snprintf(section,32,"logger:logsize");
        msq->p_ctx.logsize = iniparser_getint(ini, section,-1);
        printf("logsize = %d\n",msq->p_ctx.logsize);

        memset(section,0,sizeof(section));
        snprintf(section,32,"logger:logfile");
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->p_ctx.logfile,str,strlen(str));
        printf("logfile = %s\n",msq->p_ctx.logfile);

         memset(section,0,sizeof(section));
         snprintf(section,32,"%s:cafile",msq->p_ctx.platform);
         str = iniparser_getstring(ini,section,"NULL");
         snprintf(msq->p_ctx.cafile,sizeof(msq->p_ctx.cafile),str);
         printf("cafile = %s\n", msq->p_ctx.cafile);

         memset(section,0,sizeof(section));
         snprintf(section,32,"%s:capath",msq->p_ctx.platform);
         str = iniparser_getstring(ini,section,"NULL");
         snprintf(msq->p_ctx.capath,sizeof(msq->p_ctx.capath),str);
         printf("capath = %s\n", msq->p_ctx.capath);

         memset(section,0,sizeof(section));
         snprintf(section,32,"%s:certfile",msq->p_ctx.platform);
         str = iniparser_getstring(ini,section,"NULL");
         snprintf(msq->p_ctx.certfile,sizeof(msq->p_ctx.certfile),str);
         printf("certfile = %s\n", msq->p_ctx.certfile);

         memset(section,0,sizeof(section));
         snprintf(section,32,"%s:keyfile",msq->p_ctx.platform);
         str = iniparser_getstring(ini,section,"NULL");
         snprintf(msq->p_ctx.keyfile,sizeof(msq->p_ctx.keyfile),str);
         printf("keyfile = %s\n", msq->p_ctx.keyfile);

         iniparser_freedict(ini);

         return 0;

}

int pack_data(msq_ctx_t *msq,char *buf,int size){


        if(!msq->info_ctx.name||!msq->info_ctx.T_time||!msq->info_ctx.temperature){
                printf("get number failure\n");
                return -1;
        }
        snprintf(buf,size,"%s/%s/%f",msq->info_ctx.name,msq->info_ctx.T_time,msq->info_ctx.temperature);
        return 0;

}


#if 0
//获取cjon格式返回到buf

int get_json(msq_ctx_t *msq, char *buf)
{
        char             *msg;
        cJSON            *root;
        cJSON            *item,*item2;
        cJSON            *arr;
        char            *str1="ali";
        char            *str2="huawei";
        char            *str3="tencent";

        root =  cJSON_CreateObject();
        item =  cJSON_CreateObject();

        //memset(root, 0, sizeof(root));
        //memset(item, 0, sizeof(item));

        if(!strcmp(msq->p_ctx.platform,str1))
        {
                // cJSON_AddItemToObject(root, "method", cJSON_CreateString(st_iotd->st_mqtt.method));
                // cJSON_AddItemToObject(root, "id", cJSON_CreateString(st_iotd->st_mqtt.id));
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "params", item);
                cJSON_AddItemToObject(item, "CurrentTemperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
                // cJSON_AddItemToObject(root, "version", cJSON_CreateNumber(st_iotd->st_mqtt.version));


                // cJSON_AddItemToObject(root, "method", cJSON_CreateString(st_iotd->st_mqtt.method));
                // cJSON_AddItemToObject(root, "id", cJSON_CreateString(st_iotd->st_mqtt.id));
                // cJSON_AddItemToObject(root, "params", item);
                // cJSON_AddItemToObject(root, "time", cJSON_CreateString(st_iotd->st_collect.time));
                // cJSON_AddItemToObject(item, "CurrentTemperature", cJSON_CreateNumber(st_iotd->st_collect.temper));
                // cJSON_AddItemToObject(root, "version", cJSON_CreateNumber(st_iotd->st_mqtt.version));
        }

        if(!strcmp(msq->p_ctx.platform,str2))
        {
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                memset(item2, 0, sizeof(item2));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "services", arr = cJSON_CreateArray());
                cJSON_AddItemToArray(arr, item = cJSON_CreateObject());
                cJSON_AddItemToObject(item, "service_id", cJSON_CreateString(msq->p_ctx.service_id));
                cJSON_AddItemToObject(item, "properties", item2 = cJSON_CreateObject());
                cJSON_AddItemToObject(item2, "current_temperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
        }

        if(!strcmp(msq->p_ctx.platform,str3))
        {
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                memset(item2, 0,sizeof(item2));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "type", cJSON_CreateString(msq->p_ctx.type));
                cJSON_AddItemToObject(root, "state", item = cJSON_CreateObject());
                cJSON_AddItemToObject(item, "reported", item2 = cJSON_CreateObject());
                cJSON_AddItemToObject(item2, "temperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
                cJSON_AddItemToObject(root, "verson", cJSON_CreateNumber(msq->p_ctx.version));
                cJSON_AddItemToObject(root, "clientToken", cJSON_CreateString(msq->p_ctx.clientToken));
        }

        msg = cJSON_Print(root);
        strcpy(buf, msg);

        return 0;

}
#endif
